/*********************************************************************************
 *      Copyright:  (C) 2021 iot
 *                  All rights reserved.
 *
 *       Filename:  ts2561.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0
 *         Author:  mk
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <time.h>

#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "../lib/logger.h"
#include "tsl2561.h"

int s_tsl_fd = -1;

static const int  regs_addr[REG_COUNT]={DATA0LOW, DATA0HIGH, DATA1LOW, DATA1HIGH};

int tsl2561_init(void)
{
    if( (s_tsl_fd = open("/dev/i2c-1", O_RDWR)) < 0 )
    {
        log_err("open /dev/i2c-1 error!\n");
        return -1;
    }
    log_nrml("open /dev/i2c-1 successfully! s_tsl_fd = %d\n", s_tsl_fd);

    return s_tsl_fd;
}

int tsl2561_power(int cmd)
{ 
    struct i2c_msg               msg;
    struct i2c_rdwr_ioctl_data   data;
    unsigned char                buf[2];

    msg.addr = TSL2561_I2C_ADDR;   
    msg.flags = 0;                 
    msg.len = 1;                   
    msg.buf = buf;                 

    data.msgs = &msg;              
    data.nmsgs = 1;                

    msg.buf[0] = CONTROL_REG;
    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    {
        log_err("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
    }

    if(cmd)
    {
        msg.buf[0] = POWER_UP;
    }
    else
    {
        msg.buf[0] = POWER_DOWN;
    }

    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    {
        log_err("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
    }

    return 0;
}

int tsl2561_read_reg(unsigned char regaddr, unsigned char *regval)
{
    struct i2c_msg                  msg;
    struct i2c_rdwr_ioctl_data      data;
    unsigned char                   buf[2];

    msg.addr= TSL2561_I2C_ADDR;
    msg.flags=0;
    msg.len= 1;
    msg.buf= buf;
    msg.buf[0] = regaddr;

    data.nmsgs= 1;
    data.msgs= &msg;

    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    {
        log_err("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
    }

    memset(buf, 0, sizeof(buf));

    msg.addr= TSL2561_I2C_ADDR;
    msg.flags=I2C_M_RD;
    msg.len= 1;
    msg.buf= buf;

    data.nmsgs= 1;
    data.msgs= &msg;


    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    {
        log_err("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
    }

    *regval = msg.buf[0];

    return 0;
}

float tsl2561_get_lux( float *lux )
{
    int                 i;
    unsigned char       reg_data[REG_COUNT];
    unsigned char       buf;

    int                 chn0_data = 0;
    int                 chn1_data = 0;

    float               div = 0.0;
    

    tsl2561_power(ON);

    sleep(1);

    for(i=0; i<REG_COUNT; i++)
    {
        tsl2561_read_reg(regs_addr[i], &reg_data[i]);
    }

    chn0_data = reg_data[1]*256 + reg_data[0]; /*   Channel0 = DATA0HIGH<<8 + DATA0LOW  */
    chn1_data = reg_data[3]*256 + reg_data[2]; /*   channel1 = DATA1HIGH<<8 +  DATA1LOW */

    if( chn0_data<=0 || chn1_data<0 )
    {
        *lux = 0.0;
        goto OUT;
    }

    div = (float)chn1_data / (float)chn0_data;

    if( div>0 && div<=0.5 )
        *lux = 0.304*chn0_data-0.062*chn0_data*pow(div,1.4);

    else if( div>0.5 && div<=0.61 )
        *lux = 0.0224*chn0_data-0.031*chn1_data;

    else if( div>0.61 && div<=0.8 )
        *lux = 0.0128*chn0_data-0.0153*chn1_data;

    else if( div>0.8 && div<=1.3 )
        *lux = 0.00146*chn0_data-0.00112*chn1_data;

    else if( div>1.3 )
        *lux = 0.0;

    log_nrml("TSLl2561 get lux: [%.3f]\n", *lux);

OUT:
    tsl2561_power(OFF);
    return 0;


}
